/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ro.uvt.sp.lp.hw1.beermugs.visual;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * 
 * clasa care creaza toate elementele grafice
 * @author LP
 */
public class BeerMugsGui extends JComponent {

	
	private static final long serialVersionUID = 6292377817014834667L;
	private Canvas canvas;

	private JPanel pane;
	final private State initState;

	private String intro;

	private State curState = new BeerMugState(0, 0);
	
	/**
	 * 
	 * @param canvas
	 */
	public BeerMugsGui(Canvas canvas) {

		this.canvas = canvas;
		initState = canvas.getInitState();

		setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		initialization();
		this.canvas.repaint();
		createPanel();
	}

	
	/**
	 * metode pentru constructia elementelor grafice
	 */
	private void createPanel() {
		pane = new JPanel(new BorderLayout(15, 15));

		JButton resetButton = new JButton("Reseteaza");
		resetButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});

		JPanel resultsPanel = new JPanel();
		resultsPanel.add(resetButton);

		pane.add(resultsPanel, BorderLayout.SOUTH);
		pane.add(canvas, BorderLayout.WEST);
		pane.add(createIntro(intro), BorderLayout.NORTH);
		pane.add(createMoves(), BorderLayout.EAST);

		pane.setVisible(true);

		this.add(pane);

	}


	/**
	 * creaza operatiile ce pot fi efectuate
	 * lista de butoane va fi afisata in partea dreapta
	 * 
	 * @return
	 */
	private JComponent createMoves() {

		Move m1 = new BeerMugMove("Umple X");
		Move m2 = new BeerMugMove("Umple Y");
		Move m3 = new BeerMugMove("Goleste X");
		Move m4 = new BeerMugMove("Goleste Y");
		Move m5 = new BeerMugMove("Transfera X in Y");
		Move m6 = new BeerMugMove("Transfera Y in X");

		ArrayList<Move> list = new ArrayList<>();
		list.add(m1);
		list.add(m2);
		list.add(m3);
		list.add(m4);
		list.add(m5);
		list.add(m6);

		int rowNum = list.size();
		ArrayList<JButton> buttonList = new ArrayList<>();

		JPanel movesPanel = new JPanel(new GridLayout(rowNum, 1, 5, 3));
		for (final Move move : list) {
			final JButton but = new JButton(move.getMoveName());
			but.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					System.out.println("Button Pressed");
					
					//in functie de operatia pe care s-a facut click
					//se va executa acea operatie pe baza starii curente.
					//initial starea curenta este 0,0
					//dupa fiecare operatie curState devine noua stare calculata
					
					State newState = move.doMove(curState);
					System.out.println(newState);

					if (newState == null) {
						JOptionPane.showMessageDialog(null, "Miscare invalida");
					} else {
						canvas.setState(newState);
						canvas.repaint();
						curState = newState;

					}
				}
			});
			buttonList.add(but);
		}
		for (JButton button : buttonList) {
			movesPanel.add(button);
		}
		movesPanel.setVisible(true);
		return movesPanel;
	}

	
	/**
	 * creaza partea de introdcution text
	 * @param s
	 * @return
	 */
	private JComponent createIntro(String s) {
		JTextArea introField = new JTextArea(s);
		introField.setEditable(false);
		introField.setVisible(true);
		introField.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
		return introField;
	}

	/**
	 * 
	 */
	private void initialization() {
		intro = "Problema halbelor de bere \n\n";
	}
	
	/**
	 * 
	 */
	private void reset() {

		canvas.setState(initState);
		curState = initState;
		canvas.repaint();
	}

}
